package com.yunc.base.config;

import com.yunc.base.datasource.DynamicDataSource;
import com.yunc.base.aspect.DataSourceType;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * Configuration标识该类被纳入spring容器中实例化并管理
 * ServletComponentScan用于扫描所有的Servlet、filter、listener
 *
 * @author taocongcong
 * @create 2018-10-09 15:17
 */
@Configuration
@EnableConfigurationProperties(MybatisProperties.class)
public class DataSourceConfig {

    @Bean("masterDataSource")
    @ConfigurationProperties("spring.datasource.master")
    public DataSource masterDataSource(MybatisProperties druidProperties) {
        HikariDataSource hikariDataSource = new HikariDataSource();
        return druidProperties.dataSource(hikariDataSource);
    }

    @Bean("slaveDataSource")
    @ConfigurationProperties("spring.datasource.slave")
    @ConditionalOnProperty(prefix = "spring.datasource.slave", name = "enabled", havingValue = "true")
    public DataSource slaveDataSource(MybatisProperties druidProperties) {
        HikariDataSource hikariDataSource = new HikariDataSource();
        return druidProperties.dataSource(hikariDataSource);
    }

    @Bean("shopDataSource")
    @ConfigurationProperties("spring.datasource.shop")
    @ConditionalOnProperty(prefix = "spring.datasource.slave", name = "enabled", havingValue = "true")
    public DataSource shopDataSource(MybatisProperties druidProperties) {
        HikariDataSource dataSource = new HikariDataSource();
        return druidProperties.dataSource(dataSource);
    }

    @Bean(name = "dynamicDataSource")
    @DependsOn({ "masterDataSource", "slaveDataSource", "shopDataSource"})
    @Primary
    public DynamicDataSource dataSource(DataSource masterDataSource, DataSource slaveDataSource, DataSource shopDataSource) {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
        targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource);
        targetDataSources.put(DataSourceType.SHOP.name(), shopDataSource);
        return new DynamicDataSource(masterDataSource, targetDataSources);
    }

    /* druid页面展示处理
    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        // IP白名单
        //servletRegistrationBean.addInitParameter("allow", "192.168.1.xxx,127.0.0.1");
        // IP黑名单(共同存在时，deny优先于allow)
        //servletRegistrationBean.addInitParameter("deny", "192.168.1.xxx");
        //控制台管理用户
        // servletRegistrationBean.addInitParameter("loginUsername", "admin");
        // servletRegistrationBean.addInitParameter("loginPassword", "admin");
        //是否能够重置数据 禁用HTML页面上的“Reset All”功能
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js, *.gif, *.jpg, *.png, *.css, *.ico, /druid/*");
        return filterRegistrationBean;
    }*/
}
